#include <bits/stdc++.h>
using namespace std;

#define ONLINE_JUDGE

#ifndef ONLINE_JUDGE
#define dbg(x...)                             \
	{                                         \
		cerr << "\033[32;1m" << #x << " -> "; \
		err(x);                               \
	}
void err() 
{
	cerr << "\033[39;0m" << endl;
}
template <typename T, typename... A>
void err(T a, A... x)
{
	cerr << a << ' ';
	err(x...);
}
#else
#define dbg(...)
#endif

typedef long long LL;

const int N = 1e5 + 50, MOD = 1e9 + 7, M = 21;

int mp[2][N];
int siz[N];
int n, k, m;

int f[N][M], dp[N][M][M];
int pre[3][M];

void init()
{

	for (int i = 0; i < 10; ++i)
	{

		for (int j = 0; j < 10; ++j)
			++pre[2][i+j];
		++pre[1][i];
	}
}

int main(int argc, char const *argv[])
{
	// cout << sizeof(dp) / (1 << 20) << endl;
	init();

	// while (~scanf("%d%d%d", &n, &k, &m))
	// {
	cin >> n >> k >> m;
	memset(mp[0], -1, sizeof(int) * n);
	memset(mp[1], -1, sizeof(int) * n);
	for (int i = 0; i < n; ++i)
		siz[i] = 2;
	for (int i = 0; i < m; ++i)
	{
		int c, r, v;
		scanf("%d%d%d", &c, &r, &v);
		mp[r][c] = v;
		--siz[c];
	}

	for (int i = 0; i < n; ++i)
	{
		dbg(i, siz[i]);
		memset(f[i], 0, sizeof f[i]);
		if (siz[i] == 2)
		{
			memcpy(f[i], pre[2], sizeof(int) * M);
		}
		else if (siz[i] == 0)
		{
			f[i][mp[0][i] + mp[1][i]] = 1;
		}
		else if (siz[i] == 1)
		{
			int xx = mp[0][i] == -1 ? mp[1][i] : mp[0][i];
			// dbg(xx);
			for (int j = xx; j < xx + 10; ++j)
				f[i][j] = 1;
		}
	}

	memset(dp[0], 0, sizeof(dp[0]));
	// dbg("fuck");

	for (int i = 0; i < M; ++i)
		for (int j = 0; j < M; ++j)
			if (i + j <= k)
				dp[0][i][j] = f[0][i] * f[1][j];
	for (int i = 0; i < n - 2; ++i)
		for (int j1 = 0; j1 < M; ++j1)
			for (int j2 = 0; j2 < M; ++j2)
			{ 
				if (dp[i][j1][j2] == 0 || j1 + j2 > k || k - j1 - j2 >= M || f[i + 2][k - j1 - j2] == 0)
					continue;
				dp[i + 1][j2][k - j1 - j2] = (1LL * dp[i + 1][j2][k - j1 - j2] + 1LL * dp[i][j1][j2] * f[i + 2][k - j1 - j2]) % MOD;
			}
	int ans = 0;
	for (int i = 0; i < M; ++i)
		for (int j = 0; j < M; ++j)
		{
			if (i + j <= k)
			{
				// dbg(i, j, dp[n - 2][i][j]);

				ans = (ans + dp[n - 2][i][j]) % MOD;
			}
		}
	printf("%d\n", ans);
	// }

	// for (int i = 0; i < n; ++i)
	// 	for (int j = 0; j < M; ++j)
	// 		dbg(i, j, f[i][j]);

	return 0;
}